package com.android.functionmanagement.request.db;

import java.util.Date;

import com.android.functionmanagement.entities.CommandInfo;
import com.android.functionmanagement.globle.ConstantGloble;
import com.android.functionmanagement.util.DeviceUtils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
	
	private final static int VERSION = 1;
	private final static String DB_NAME = "location.db";
	private final static String TABLE_NAME = "commandinfo";
	public final static int TABLE_COMMADINFO_FLAG = 10001;
	private final static String CREATE_TBL = "create table CommandInfo(_id integer primary key autoincrement, _type text, _content text not null, _time date)";
	private SQLiteDatabase db;

	//SQLiteOpenHelper子类必须要的一个构造函数
	public DBHelper(Context context, String name, CursorFactory factory,int version) {
		//必须通过super 调用父类的构造函数
		super(context, name, factory, version);
	}
	
	//数据库的构造函数，传递三个参数的
	public DBHelper(Context context, String name, int version){
		this(context, name, null, version);
	}
	
	//数据库的构造函数，传递一个参数的， 数据库名字和版本号都写死了
	public DBHelper(Context context){
		this(context, DB_NAME, null, VERSION);
	}
	
    // 回调函数，第一次创建时才会调用此函数，创建一个数据库
	@Override
	public void onCreate(SQLiteDatabase db) {
		this.db = db;
		db.execSQL(CREATE_TBL);
	}

	//回调函数，当你构造DBHelper的传递的Version与之前的Version调用此函数
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}
	
	//插入方法
	public long insert(CommandInfo info,int type){
		//获取SQLiteDatabase实例
		SQLiteDatabase db = getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("_type", info._type);
		values.put("_content", info._content);
		values.put("_time", DeviceUtils.formatDate(info._time,"yyyyMMddhhmmss"));
		try {
			switch (type) {
			case TABLE_COMMADINFO_FLAG:
				//插入数据库中
				return db.insert(TABLE_NAME, null, values);
			default:
				break;
			}
		} catch (Exception e) {
			
		}finally{
			db.close();
		}
		return -1;
	}
	
	//查询方法
	public Cursor query(int type){
		SQLiteDatabase db = getReadableDatabase();
		Cursor c = null;
		switch (type) {
		case TABLE_COMMADINFO_FLAG:
			//获取Cursor
			c = db.query(TABLE_NAME, null, null, null, null, null, null, null);
			break;
		default:
			break;
		}
		return c;
		
	}
	
	//根据唯一标识_id  来删除数据
	public void delete(int id,int type){
		SQLiteDatabase db = getWritableDatabase();
		switch (type) {
		case TABLE_COMMADINFO_FLAG:
			db.delete(TABLE_NAME, "_id=?", new String[]{String.valueOf(id)});
			break;
		default:
			break;
		}
		
	}
	
	
	//更新数据库的内容
	public void update(ContentValues values, String whereClause, String[]whereArgs,int type){
		SQLiteDatabase db = getWritableDatabase();
		switch (type) {
		case TABLE_COMMADINFO_FLAG:
			db.update(TABLE_NAME, values, whereClause, whereArgs);
			break;
		default:
			break;
		}
	}
	
	//关闭数据库
	public void close(){
		if(db != null){
			db.close();
		}
	}

}
